//
//  YCVistorView.swift
//  YCWeibo009
//
//  Created by user on 15/11/30.
//  Copyright © 2015年 user. All rights reserved.
//

import UIKit


//协议
protocol YCVistorViewDelegate: NSObjectProtocol{
    //登入按钮点击
    func vistorViewLoginClick()

}

 //访客视图
class YCVistorView: UIView {
    
     //MARK: - 属性
    ///代理
    weak var delegate: YCVistorViewDelegate?
    //swift默认所有的view都能通过xib/storyboard加载
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
       //super是要在设置普通存储型属性的下面
        super.init(frame: frame)
        
        //设置背景颜色,
        //backgroundColor = UIColor(red: 237 / 255.0, green: 237 / 255.0, blue: 237 / 255.0, alpha: 1)
        //当三种颜色都一样是可以用下面的方法
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        prepareUI()
    }
    
    // MARK: - 按钮点击事件
    func loginClick(){
       //model 另外一个控制器
        //通知代理
        delegate?.vistorViewLoginClick()
    }
    
    
    // MARK: - 转轮动画
    func rotationAnimation(){
        //核心动画，旋转
        let rotation = CABasicAnimation(keyPath: "transform.rotation")
        
        //设置动画参数
        rotation.toValue = 2 * M_PI
        rotation.duration = 20  //转的时间
        rotation.repeatCount = MAXFLOAT //设置它一直转
        
        //点击其他地方时不要移除动画
        rotation.removedOnCompletion = false
        
        
        //开始核心动画
        iconView.layer.addAnimation(rotation, forKey: "rotation")
    }
    
    // MARK: - 核心动画开始和暂停
    /// 暂停旋转
    func pauseAnimation() {
        // 记录暂停时间
        let pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        
        // 设置动画速度为0
        iconView.layer.speed = 0
        
        // 设置动画偏移时间
        iconView.layer.timeOffset = pauseTime
    }
    
    /// 恢复旋转
    func resumeAnimation() {
        // 获取暂停时间
        let pauseTime = iconView.layer.timeOffset
        
        // 设置动画速度为1
        iconView.layer.speed = 1
        
        // 重置偏移时间
        iconView.layer.timeOffset = 0
        
        // 重置开始时间
        iconView.layer.beginTime = 0
        
        // 计算开始时间
        let timeSincePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        
        // 设置开始时间
        iconView.layer.beginTime = timeSincePause
    }
    
    
    // MARK: - 设置访客视图的内容
    /**
        设置访客视图的内容
    - parameter imageName: 图片名称
    - parameter message:    内容
    */
    func setupVistorView(imageName: String, message: String){
        //图片,替换转轮的图片
        iconView.image = UIImage(named: imageName)
        
        //文字
        messageLabel.text = message
        //适应大小
        messageLabel.sizeToFit()
        
        //隐藏小房子
        homeView.hidden = true
        
        coverView.hidden = true
    }
    
    //准备UI
    private func prepareUI(){
        //1.添加 子空间
        addSubview(iconView)
        //添加遮罩
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //2.添加约束 autolayout
        //注意：要设置约束的view有父控件后才能设置它的自动布局约束
//        关闭Autoresizing
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        //创建约束对象,
        ///iconView CenterX与父控件的CenterX重合
        /*
            item:要添加约束的view
            attribute:设置的约束
            relatedBy:Equal
            toItem:参照的view
            attribute:参照的view的约束
        */
       addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute:NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
           
        ///iconView CenterY与父控件的CenterY重合
        addConstraint(   NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))

        //小房子,X
         addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy:
            NSLayoutRelation.Equal, toItem: iconView, attribute:NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //y
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute:NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //消息 label X
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute:NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        // Y 
         addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute:NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        //宽度约束
        //当toItem = nil时attribute必须设置未NSLayoutAttribute.NotAnAttribute
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 226))
        //注册按钮
        //左边和label对象
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute:NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        //顶部距离label底部16
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute:NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //宽度
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
        //高度
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //登入按钮
        //右边和label对象
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute:NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        //顶部距离label底部16
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute:NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //宽度
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
        //高度
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        //遮罩约束
        //左
        addConstraint( NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        //上
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        //右边
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        //下边
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    }
    
    // MARK: - 懒加载
    ///转轮
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    ///小房子
    private lazy var homeView:UIImageView =  UIImageView(image: UIImage(named:"visitordiscover_feed_image_house"))
    
    ///消息label
    private lazy var messageLabel:UILabel = {
        let label = UILabel()
        
        //设置文本颜色
        label.textColor = UIColor.darkGrayColor()
        
        //设置label的文本
//        label.text = "加关注，更多精彩"
        label.text = "加关注，更多精彩加关注，更多精彩"

        //根据文本来适应大小
        label.sizeToFit()
        label.numberOfLines = 0
        return label
    }()
    
    ///注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        //设置按钮的图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        //设置本文
        button.setTitle("注册", forState: UIControlState.Normal)
        
        //设置文本颜色
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        //设置文本大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        //根据内容适应大小
        button.sizeToFit()
        
        return button
    }()
    
    ///登入按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        //设置按钮的图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        //设置本文
        button.setTitle("登入", forState: UIControlState.Normal)
        //设置文本颜色
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        //设置文本大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        //根据内容适应大小
        button.sizeToFit()
        
        //添加点击事件
        button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        }()
    
    ///懒加载，遮罩视图
    private lazy var coverView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    
}
